$(function() {
        	
	var service = new EbeimService();
	var op = $(".form-group");
	var form = $("#j-form");
	var jbindDevice = $("#j-bindDevice");
	var jbindDeviceName = $("#j-bindDeviceName");
	
	op.each(function() {
        var t = $(this),
            input = t.find(".required"),
            name = input.attr("name"),
            holder = input.siblings(".message");
        input.blur(function() {
            activeCheck(form,name);
        }).focus(function() {
            holder.show();
        });;
    });
	
	service.getComputerMessage(function(json){
		if(json.code == 200) {
			jbindDevice.html(json.bindDevice);
			jbindDevice.siblings("[name='bindDevice']").val(json.bindDevice);
			jbindDeviceName.val(json.bindDeviceName);
		}else{
            alert(json.desc);
        }
	})
	
	form.delegate("#j-submit","click",function () {
        if (!activeCheck(form)) {
        	return false;
        }else{
        	var labType = GetQueryString("labType");
            var data = form.serialize();
            
            data = data + "&username="+ GetQueryString("username");
            
            if(labType == "3"){
            	service.keyBindCheck(data, function(json){
                    if(json.code == 200) {
                        alert(json.desc);
                        window.location.href = "../index.html";
                    }else{
                    	alert(json.desc);
                    }
                });
            }else{
            	service.authorizationCodeBindCheck(data, function(json){
                    if(json.code == 200) {
                        alert(json.desc);
                        window.location.href = "../index.html";
                    }else{
                    	alert(json.desc);
                    }
                });
            }
            
        }
    })
});


//激活验证
var activeCheck = function(form,name) {

	// 显示错误信息
    var showError = function(obj, msg) {
        var msgDom = obj.siblings('.message');
        var msgTxt = msgDom.children('i')
        msgDom.addClass("msg-error").removeClass('msg-tip');
        msgTxt.html(msg);
        msgDom.show();
    };

    // 隐藏错误信息
    var hideError = function(obj, msg) {
        var msgDom = obj.siblings('.message');
        var msgTxt = msgDom.children('i')
        msgDom.hide();
        msgDom.addClass("msg-tip").removeClass('msg-error');
        msgTxt.html(msg);
    };

    // 数据验证对象
    var dataCheck = {
    	bindDeviceName: {
            obj: form.find("[name='bindDeviceName']"),
            fn: function() {
                var val = this.obj.val();
                if (formCheck.isNull(val)) {
                    showError(this.obj,"请输入设备名称");
                    return false;
                } else {
                    hideError(this.obj,"请输入设备名称");
                    return true;
                }
            }
        }
    };

    if (name && dataCheck[name]) {
        return dataCheck[name].fn();
    } else {
        var errLen = 0;
        for (var i in dataCheck) {
            if (!dataCheck[i].fn()) {
                errLen++;
            }
        }
        if (errLen > 0) {
            return false;
        }
        return true;
    }
}